package com.ruoyi.mes.controller;

import java.math.BigDecimal;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.mes.domain.MesProcessExec;
import com.ruoyi.mes.service.IMesProcessExecService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.mes.domain.MesTasks;
import com.ruoyi.mes.service.IMesTasksService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 生产任务管理Controller
 * 
 * @author ruoyi
 * @date 2021-12-07
 */
@RestController
@RequestMapping("/tasks")
public class MesTasksController extends BaseController
{
    @Autowired
    private IMesTasksService mesTasksService;
    @Autowired
    private IMesProcessExecService mesProcessExecService;
    /**
     * 查询生产任务管理列表
     */
    @PreAuthorize(hasPermi = "mes:tasks:list")
    @GetMapping("/list")
    public TableDataInfo list(MesTasks mesTasks)
    {
        startPage();
        List<MesTasks> list = mesTasksService.selectMesTasksList(mesTasks);
        return getDataTable(list);
    }

    /**
     * 导出生产任务管理列表
     */
    @PreAuthorize(hasPermi = "mes:tasks:export")
    @Log(title = "生产任务管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MesTasks mesTasks) throws IOException
    {
        List<MesTasks> list = mesTasksService.selectMesTasksList(mesTasks);
        ExcelUtil<MesTasks> util = new ExcelUtil<MesTasks>(MesTasks.class);
        util.exportExcel(response, list, "生产任务管理数据");
    }

    /**
     * 获取生产任务管理详细信息
     */
    @PreAuthorize(hasPermi = "mes:tasks:query")
    @GetMapping(value = "/{taskUuid}")
    public AjaxResult getInfo(@PathVariable("taskUuid") String taskUuid)
    {
        MesTasks a = mesTasksService.selectMesTasksByTaskUuid(taskUuid);
        return AjaxResult.success(a);
    }

    /**
     * 新增生产任务管理
     */
    @Transactional
    @PreAuthorize(hasPermi = "mes:tasks:add")
    @Log(title = "生产任务管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MesTasks mesTasks)
    {

        MesTasks m = new MesTasks();
        m.setDocNum(mesTasks.getDocNum());
        List<MesTasks> listByDocNum = mesTasksService.selectMesTasksList(m);
        if(listByDocNum.size()>0){
            return  new AjaxResult(1521,"生产任务编号已存在！请重新填写！");
        }else{
            mesTasks.setCreateBy(SecurityUtils.getUsername());
            mesTasks.setCreateTime(DateUtils.getNowDate());
            // 获取当前汇报数量
            BigDecimal curQty = mesTasks.getTaskQty();

            // 获取剩余可汇报数量
            BigDecimal remainQty = mesTasksService.selectRemainTaskQty(mesTasks);
            // 剩余可汇报数量和当前汇报数量做比较
            if(curQty.compareTo(remainQty)>0){
                return new AjaxResult(1501,"您当前投放的数量为 "+curQty+"，"+"超出了剩余可投放数量："+remainQty+"！");
            }else{
                mesTasksService.insertMesTasks(mesTasks);
                MesProcessExec mesProcessExec = new MesProcessExec();
                mesProcessExec.setFkMesTaskUuid(mesTasks.getTaskUuid());
                mesProcessExec.setFkAttentionUserId(mesTasks.getFkAttentionUserId());
                mesProcessExec.setExecQty(mesTasks.getTaskQty());
                mesProcessExec.setDocNum(mesTasks.getDocNum().replace("TSK","PRE"));
                mesProcessExec.setDocDt(mesTasks.getDocDt());
                mesProcessExec.setFkMesProcessUuid(mesTasks.getFkMesProcessUuid());
                int r = 0;
                mesProcessExec.setCreateBy(SecurityUtils.getUsername());
                mesProcessExecService.genProcessExec(mesProcessExec);
                if("success".equals(mesProcessExec.getResultMsg())){
                    r=1;
                }
                return toAjax(r);
            }
        }
    }

    /**
     * 修改生产任务管理
     */
    @PreAuthorize(hasPermi = "mes:tasks:edit")
    @Log(title = "生产任务管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MesTasks mesTasks)
    {
        mesTasks.setUpdateBy(SecurityUtils.getUsername());
        mesTasks.setUpdateTime(DateUtils.getNowDate());
        // 获取当前汇报数量
        BigDecimal curQty = mesTasks.getTaskQty();

        // 获取剩余可汇报数量
        BigDecimal remainQty = mesTasksService.selectRemainTaskQty(mesTasks);
        // 剩余可汇报数量和当前汇报数量做比较
        if(curQty.compareTo(remainQty)>0){
            return new AjaxResult(1521,"您当前投放的数量为 "+curQty+"，"+"超出了剩余可投放数量："+remainQty+"！");
        }else{
            return toAjax(mesTasksService.updateMesTasks(mesTasks));
        }


    }

    /**
     * 删除生产任务管理
     */
    @PreAuthorize(hasPermi = "mes:tasks:remove")
    @Log(title = "生产任务管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{taskUuids}")
    public AjaxResult remove(@PathVariable String[] taskUuids)
    {
        return toAjax(mesTasksService.deleteMesTasksByTaskUuids(taskUuids));
    }
}
